package com.baibei.ebec.user.messagefollow;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baibei.ebec.user.R;
import com.baibei.ebec.user.R2;
import com.baibei.model.FollowMessageContentInfo;
import com.baibei.model.FollowMessageInfo;
import com.baibei.model.FollowMsgInfo;
import com.baibei.model.UserInfo;
import com.baibei.module.AppRouter;
import com.baibei.module.BasicFragment;
import com.baibei.ui.AppUI;
import com.baibei.widget.CommonRefreshLayout;
import com.blankj.utilcode.utils.LogUtils;
import com.rae.swift.session.SessionManager;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by tang on 2018/8/16.
 */

public class MessageFollowFragment extends BasicFragment implements MessageFollowContract.MessageFollowView {
    @BindView(R2.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R2.id.refresh_layout)
    CommonRefreshLayout refreshLayout;
    Unbinder unbinder;
    private ArrayList<FollowMsgInfo> msgInfos;
    private MessageFollowContract.Presenter mPresenter;
    private MessageFollowAdapter adapter;
    private String userId;

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.fragment_system_message_follow;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        msgInfos = new ArrayList<>();
        mPresenter = inject(MessageFollowContract.Presenter.class);
        UserInfo userInfo = SessionManager.getDefault().getUser();
        userId = userInfo.getUserId() + "";
        mPresenter.getMsgList(userId);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        adapter = new MessageFollowAdapter(getContext());
        adapter.setToptextGravity();

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);

        refreshLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                mPresenter.getMsgList(userId);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void hideRefreshLayout() {
        if (refreshLayout != null && refreshLayout.isRefreshing()) {
            refreshLayout.refreshComplete();
        }
    }

    @Override
    public void onLoadMsg(List<FollowMessageInfo> followMsgInfos) {
        msgInfos.clear();
        for (FollowMessageInfo info : followMsgInfos){
            List<FollowMessageContentInfo> detail = info.getDetail();
            for (FollowMessageContentInfo content : detail){
                msgInfos.add(new FollowMsgInfo(timeStrToSecond(info.getDate())+"",content.getMessage(),content.getTime(),content.getState(),content.getType(),content.getFollowerId(),content.getUserPic()));
            }
        }
        adapter.setInfos(msgInfos);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showLoading() {
        AppUI.loading(getContext());
    }

    @Override
    public void hideLoading() {
        AppUI.dismiss();
    }

    public Long timeStrToSecond(String time) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Long second = format.parse(time).getTime();
            return second;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return -1l;
    }
}
